

import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_notification_center.dart';
import 'package:bt_tool/basic/bt_router.dart';
import 'package:bt_tool/basic/bt_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_demo/tools/project/project_tool.dart';
import 'package:get/get.dart';

class ToolsMainController extends BTController with BTRouterTabbarProtocol{

  //   push后如果是保留tab则用这种方法
  //    * Navigator(
  //       onGenerateInitialRoutes: (navigator, initialRoute){
  //       return [
  //       MaterialPageRoute(builder: (c)=> const ToolsDoNextPage()),
  //       ];
  //       },
  //       ),
  var selectIndex = 0.obs;
  List<Widget> listWidget = [];
  late List<String> routerNameList;
  Map<String,BTRouterConfig> routerConfigCache = {};
  BTRouterConfig? currentRouterConfig;
  double leftContentWidth = 100;
  final List<String> tabbarNameList = [
    "Encode",
    "JsonTo",
    "Encrypt",
    "DoNext",
    "CreditList",
    "CopyPast",
    "FImgPath",
    "Project",
    "JsonPre"
  ];

  @override
  void initState() {
    super.initState();
    BTRouter.share().tabProtocol = this;
    routerNameList = BTRouter.logic().getTabPageRouterNameList();
    for(var str in routerNameList){
      listWidget.add(const SizedBox());
    }
    String routerName = routerNameList.first;
    BTRouter.share().pushTo(routerConfig: getRouterConfig(routerName));
  }

  @override
  void btRouterAction(BTRouterConfig routerConfig) {
    if(btIsStrEmpty(routerConfig.routerName)){
      return;
    }
    String routerName = routerConfig.routerName!;
    int index = routerNameList.indexOf(routerName);
    Widget widget = listWidget[index];
    if(widget is SizedBox){
      var tabbarWidget = BTRouter.share().routerProtocol.getWidgetWithRouter(routerConfig);
      if(tabbarWidget != null){
        listWidget[index] = tabbarWidget;
      }
    }
    selectIndex.value = index;
    lifeCycleLogic(routerConfig);
  }

  itemClick(int index){
    String routerName = routerNameList[index];
    BTRouter.share().pushTo(routerConfig: getRouterConfig(routerName));
  }

  lifeCycleLogic(BTRouterConfig routerConfig){
    if(currentRouterConfig == null){
      routerConfig.didAppearCallback();
      currentRouterConfig = routerConfig;
      return;
    }

    currentRouterConfig!.didDisAppearCallback();
    routerConfig.didAppearCallback();
    currentRouterConfig = routerConfig;
  }

  BTRouterConfig getRouterConfig(String routerName){
    BTRouterConfig? routerConfig = routerConfigCache[routerName];
    if(routerConfig == null){
      routerConfig = BTRouterConfig(routerName: routerName);
      routerConfigCache[routerName] = routerConfig;
    }
    return routerConfig;
  }


}